<?php



class Backup extends Database {

	
	
	public static function doBackup(){
		$schema=self::getSchema();
        
		$backup='';
		$requete="SELECT * FROM information_schema.tables WHERE table_schema='$schema'";
		$resultTable=self::query($requete);
		while($tupleTable=mysqli_fetch_assoc($resultTable)){
			$table=$tupleTable['TABLE_NAME'];
			$backup.="TRUNCATE TABLE `$table`;".PHP_EOL;
			
			$requete='SELECT * FROM information_schema.columns '.
				"WHERE table_schema='$schema' and table_name='$table'";
			
			$resultColumn=self::query($requete);
			
			$typesColonnes=array();
			$columns='';
			while($tupleColumn=mysqli_fetch_assoc($resultColumn)){
				$colonne=$tupleColumn['COLUMN_NAME'];
				if($columns!='') $columns.=', ';
				$columns.="`$colonne`";
				$typesColonnes[$colonne]=$tupleColumn['DATA_TYPE'];
			}
			
			$requete="SELECT $columns FROM `$table`";
			$resultValues=self::query($requete);
			
			$values='';
			while($tupleValue=mysqli_fetch_assoc($resultValues)){
				if($values!='') $values.=',';
				$ligne='';
				foreach($tupleValue as $col => $val){
					if($ligne!='') $ligne.=', ';
					if($typesColonnes[$col]!='int'){
						$val="'".str_replace("'","''",$val)."'";
					}
					$ligne.=$val;
				}
				$values.="($ligne)";
			}
			if($values!=''){
				$backup.="INSERT INTO `$table` ($columns) VALUES ";
				$backup.=$values.';'.PHP_EOL;
			}
		}
		return $backup;
	}
	
	
	public static function dump(){
        $schema=self::getSchema();
        
		$encode='utf8_general_ci';
		$charset='utf8';
		
		$dump='SET AUTOCOMMIT = 0;'.PHP_EOL;
		$dump.='START TRANSACTION;'.PHP_EOL;
		$dump.="CREATE DATABASE IF NOT EXISTS $schema DEFAULT CHARACTER SET $charset COLLATE $encode;".PHP_EOL;
		$dump.="USE $schema;".PHP_EOL;
		
		$requete="SELECT * FROM information_schema.tables WHERE table_schema='$schema'";
		$resultTable=self::query($requete);
		while($tupleTable=mysqli_fetch_assoc($resultTable)){
			$table=$tupleTable['TABLE_NAME'];
			
			$requete='SELECT * FROM information_schema.columns '.
				"WHERE table_schema='$schema' and table_name='$table'";
			
			$resultColumn=self::query($requete);
			
			$pk='';
			$columns='';
			while($tupleColumn=mysqli_fetch_assoc($resultColumn)){
				$name=$tupleColumn['COLUMN_NAME'];
				$type=$tupleColumn['COLUMN_TYPE'];
				$null=$tupleColumn['IS_NULLABLE']=='NO'?'NOT NULL':'';
				$default=$tupleColumn['COLUMN_DEFAULT']!=null?
						'DEFAULT \''.$tupleColumn['COLUMN_DEFAULT'].'\'':'';
				$comment=$tupleColumn['COLUMN_COMMENT']!=''?
						'COMMENT \''.str_replace("'","''",$tupleColumn['COLUMN_COMMENT']).'\'':'';
				$extra=$tupleColumn['EXTRA']!=''?$tupleColumn['EXTRA']:'';
				if($tupleColumn['COLUMN_KEY']=='PRI'){
					if($pk!='') $pk.=',';
					$pk.=$name;
				}
				$columns.="$name $type $null $default $comment $extra, ";
			}
			
			$engine=$tupleTable['ENGINE'];
			$comment=$tupleTable['TABLE_COMMENT']!=''?
					'COMMENT=\''.str_replace("'","''",$tupleTable['TABLE_COMMENT']).'\'':'';
			$autoI=$tupleTable['AUTO_INCREMENT']!=''?'AUTO_INCREMENT='.$tupleTable['AUTO_INCREMENT']:'';
			
			$dump.="DROP TABLE IF EXISTS $table;".PHP_EOL;
			$dump.="CREATE TABLE $table ( $columns PRIMARY KEY ($pk) ) ".
				"ENGINE=$engine DEFAULT CHARSET=$charset $comment $autoI ;".PHP_EOL;

		}
		$dump.='COMMIT;'.PHP_EOL;
		return $dump;
	}
	
	
	public static function restore($script){
		Message::log('restore de la base de données');
		self::query('SET AUTOCOMMIT = 0;');
		self::query('START TRANSACTION;');
		$ok=true;
		foreach(FileSystem::readFile($script) as $commande){
			if(strlen($commande)!=0){
				if(!self::query(Texte::toUTF8($commande))){
					$ok=false;
				}
			}
		}
		if($ok){
			self::query('COMMIT;');
			Message::log('Restore ok');
		}
		else{
			self::query('ROLLBACK;');
			Message::ERROR('La restauration de la base de données a échoué');
		}
		return $ok;
	}


}

?>